<?php

/**
 *
 * @version $1.0$
 * @copyright 2011
 */
class Base_FileEditorObject extends Erra_Fields_Base_Object {

    public $localizableSource = null;
    public $editableFileList = array();
    public $defaultFile = array();

    /**
     * locale::__construct()
     *
     * @param mixed $buildArgs
     */
    function __construct($objectId, $buildArgs = null) {
        parent::__construct($objectId, $buildArgs);

        $this->getEditableFileList($buildArgs);

        $developerChangeItem = Erra_Fields_ComboBox::create('developerChangeItem')->setDescription(__('Change view'))->setRawData($this->defaultFile)->fill($this->editableFileList);


        $currentFile = Erra_Fields_Hidden::create('currentFile')
                ->setDefaultValue($this->defaultFile);
        $changeFile = Erra_Fields_Hidden::create('changeFile')
                ->setDefaultValue(0);
        $editareaZone = Erra_Fields_TextArea::create('editareaZone')
                ->setRows(15)
                ->setCols(127)
                ->setAttribute('class', 'editareaZone');

        $this->addFields($currentFile, $changeFile, $developerChangeItem, $editareaZone);
        $this->setAttributes(array('style' => 'width:1000px;'));
        $this->setActionName(null);
        $this->setReadOnly(true);
    }

    public function _form($actionMethod) {
        // Load current file before render the form
        $currentItemFile = self::url_to_filepath($this->currentFile->getRawData());
        $currentItemContents = file_get_contents(DOCUMENT_ROOT . '/' . $currentItemFile);
        $this->editareaZone->setRawData($currentItemContents);
        
        return parent::_form($actionMethod);
    }

    function getEditableFileList($directory) {
        $directory = strtolower($directory);
        $extension = ($directory == 'objects') ? '.php' : '.phtml';
        $itemList = Erra_Core_File::spl_scanRecursiveDir(DOCUMENT_ROOT . '/application/' . ucfirst($directory), $extension);
        $data = array();
        $defaultFile = null;
        foreach ($itemList as $itemFile) {
            $forbidden_names = array('translation.', ucfirst($directory) . '/Admin', ucfirst($directory) . '\Admin', 'Objects\Base', 'Objects\Std', 'Objects/Base', 'Objects/Std');

            if (strstrarr($itemFile, $forbidden_names) === FALSE) {
                $itemFile = str_replace(DOCUMENT_ROOT, '', $itemFile);
                $defaultFile = is_null($defaultFile) ? Base_LocaleObject::filepath_to_url($itemFile) : $defaultFile;
                $data[Base_LocaleObject::filepath_to_url($itemFile)] = basename($itemFile);
            }
        }
        $this->editableFileList = $data;
        $this->defaultFile = $defaultFile;
        return $this->editableFileList;
    }

    public static function getCurrentEditFile() {
        
    }

    public static function filepath_to_url($input) {
        $input = str_replace('/', '-sl-', $input);
        $input = str_replace(trim('\ '), '-asl-', $input);
        $input = str_replace('.', '-dot-', $input);
        $input = str_replace(':', '-dbdot-', $input);
        return $input;
    }

    public static function url_to_filepath($input) {
        $input = str_replace('-sl-', '/', $input);
        $input = str_replace('-asl-', trim('\ '), $input);
        $input = str_replace('-dot-', '.', $input);
        $input = str_replace('-dbdot-', ':', $input);
        return $input;
    }

}

?>